package com.justdoit.facedemo.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.justdoit.facedemo.base.Results;
import com.justdoit.facedemo.domain.Meeting;
import com.justdoit.facedemo.domain.MeetingVo;
import com.justdoit.facedemo.domain.User;
import com.justdoit.facedemo.mapper.MeetingMapper;
import com.justdoit.facedemo.service.MeetingService;
import com.justdoit.facedemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/meeting")
public class MeetingController {

    //注入MeetingService
    @Autowired
    private MeetingService meetingService;
    @Resource
    private MeetingMapper meetingMapper;

    //对会议信息新增和修改
    @PostMapping("/insertOrUpdate")
    public boolean saveUserOrUpdate(@RequestBody Meeting meeting){
        return meetingService.saveUser(meeting);
    }

    //会议的审核
    @PostMapping("/MyUpdate")
    //修改会议的状态为取消
    public boolean MyUpdate(@RequestBody Meeting meeting){
        if (meeting !=null)return meetingService.updateById(meeting);
        return false;
    }
   //查询单条会议
    @GetMapping("/SelectOneMeeting")
    public List<MeetingVo> selectOneMeeting(){
        List<MeetingVo> meetingList = meetingMapper.selectOneMeeting();
        return meetingList;
    }
    //根据id删除会议信息
    @DeleteMapping("/{id}")
    public boolean deleteById(@PathVariable Integer id ){
        //调用mybatis plus的通过id删除的方法实现删除
      return meetingService.removeById(id);
    }
    //根据Id批量删除会议信息
    @PostMapping("del/batch")
    public boolean batchDeleteById(@RequestBody List<Integer> ids ){
        //调用mybatis plus的通过id删除的方法实现删除
        return meetingService.removeBatchByIds(ids);
    }
    //查询所有数据 导出为excel
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        //查出所有的数据
        List<Meeting> list = meetingService.list();
        //调用工具类
        ExcelWriter writer = ExcelUtil.getWriter(true);
        //自定义标题别名
        writer.addHeaderAlias("meetingId","会议id");
        writer.addHeaderAlias("userId","会议召开者Id");
        writer.addHeaderAlias("state","会议的状态");
        writer.addHeaderAlias("conferenceTheme","会议主题描述");
        writer.addHeaderAlias("contentDescription","会议内容描述");
        writer.addHeaderAlias("address","开会的地方");
        writer.addHeaderAlias("startTime","会议开始时间");
        writer.addHeaderAlias("endTime","会议结束时间");
        //写出到excel 使用默认样式,强制输出标题
        writer.write(list,true);
        //设置浏览器的响应格式
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("会议信息表-" + DateUtil.today() + ".xls", "utf-8"));
        //输出流
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        writer.close();

    }
    //mybatis plus分页查询
    @GetMapping("/findPage")
    public Map<String,Object> findPage(@RequestParam Integer pageNum,
                                       @RequestParam Integer pageSize,
                                       //如果前端没有传值就默认为空
                                       @RequestParam @DefaultValue("") String user_name,
                                       @RequestParam String address){
        //页面换算
        pageNum = (pageNum-1)*pageSize;
        //模糊查询拼接
        if(!"null".equals(user_name)){
           user_name = "%"+user_name+"%";
        }
        if(!"null".equals(address)){
            address = "%"+address+"%";
        }
        //查询到的会议信息
        List<MeetingVo> MeetingPage = meetingMapper.selectAllMeeting(user_name,address,pageNum,pageSize);
        //查询该条件下的总数据量
        Integer total = meetingMapper.selectTotal(user_name, address);
        //把拿到数据放在map
        HashMap<String, Object> res = new HashMap<>();
        res.put("dada",MeetingPage);
        res.put("total",total);
        //返回
        return res ;
    }



    //对会议状态分页查询
    @GetMapping("/findPage2")
    public  IPage<Meeting> findPage2(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize,
                                    //如果前端没有传值就默认为空
                                    @RequestParam(defaultValue = "") String user_name,
                                    @RequestParam(defaultValue = "") String address,
                                    @RequestParam(defaultValue = "未审核") String state){
        //page对象
        IPage<Meeting> page2 = new Page<>(pageNum,pageSize);
        //模糊查询拼接
        QueryWrapper<Meeting> queryWrapper = new QueryWrapper<>();
        //判断非空情况
        if(!"".equals(user_name)){
            queryWrapper.like("user_name",user_name);
        }
        if(!"".equals(address)){
            queryWrapper.like("address",address);
        }
        if(!"".equals(state)){
            queryWrapper.like("state",state);
        }
        IPage<Meeting> userPage = meetingService.page(page2, queryWrapper);
        return userPage;
    }
}
